﻿<?php


class QueuetypeApp extends BaseApp
{

    var $type = 'queuetype';
	
    function index()
    {

			
		$TB_new = &$this->_model('queuetype');
				
		$data = $TB_new->show();	

        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);	
									
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('queuetype_list.view.html');
    }

    function add()
    {	
	
	    $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();
		$moudedata = array();	
		foreach($module_list_data as $key => $value)
		{
		    $moudedata[$key]['id'] = $value['id'];
			$moudedata[$key]['alias'] = $value['alias'];
			$moudedata[$key]['appname'] = $value['appname'];
			$moudedata[$key]['category'] = & $GLOBALS['SCup']->_get_category_data($value['appname']);
		}
		
        $this->assign('moudedata', $moudedata);		
		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('queuetype_add.view.html');
    }


    function addpost()
    {

	     $data = $_POST['data'];
   	     if(empty($data['queuemoney']))
		 {	
		      $notice = &get_message(array('msg'=>"面值填写完才能提交",'url'=>'','title'=>'Notice'));
		      $notice ->display();		
	     }
   	     if(!is_numeric($data['queuemoney']))
		 {	
		      $notice = &get_message(array('msg'=>"面值只能填数字",'url'=>'','title'=>'Notice'));
		      $notice ->display();		
	     }
		 		 
         $tcid = $data['tcid'];
		 unset($data['tcid']);
		 $tempdata = del_empty_var(explode('|', $tcid));
		 $tempdata1 = array();
		 $selectstr = '';
		 foreach($tempdata as $key=>$value)
		 {
			$temp = explode(":",$value);
			$tempdata1[$key]['id'] = $temp[0];
			$tempdata1[$key]['name'] = $temp[1];
		 }
		 if(isset($tempdata1[0])&&isset($tempdata1[0]['id']))
		 {
		 $data['table_id'] =  $tempdata1[0]['id'];
		 $data['tablename'] =  $tempdata1[0]['name'];		 
		 }		 	

		 if(isset($tempdata1[1])&&isset($tempdata1[1]['id']))
		 {
		    $data['cat_id'] =  $tempdata1[1]['id'];
		    $data['catname'] =  $tempdata1[1]['name'];			 
		 }
	 
         $data['add_time'] = time();
		 
		 $TB_new = &$this->_model('queuetype'); 
		 		 		
		 $TB_new->post($data);
		 		 
		 $this->index();
    }


    function edit()
    {	
		$id= intval($_GET['id']);
		
		$TB_new = &$this->_model('queuetype');
				
		$data = $TB_new->oneshow($id);		
	    $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();
		$moudedata = array();	
		foreach($module_list_data as $key => $value)
		{
		    $moudedata[$key]['id'] = $value['id'];
			$moudedata[$key]['alias'] = $value['alias'];
			$moudedata[$key]['appname'] = $value['appname'];
			$moudedata[$key]['category'] = & $GLOBALS['SCup']->_get_category_data($value['appname']);
		}
		
        $this->assign('moudedata', $moudedata);	


        $this->assign('data', $data);

        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('queuetype_edit.view.html');   
    }





    function editpost()
    {

		$id= intval($_POST['id']);
		
		$data = $_POST['data'];
   	     if(empty($data['queuemoney']))
		 {	
		      $notice = &get_message(array('msg'=>"面值填写完才能提交",'url'=>'','title'=>'Notice'));
		      $notice ->display();		
	     }
   	     if(!is_numeric($data['queuemoney']))
		 {	
		      $notice = &get_message(array('msg'=>"面值只能填数字",'url'=>'','title'=>'Notice'));
		      $notice ->display();		
	     }
		 		 
         $tcid = $data['tcid'];
		 unset($data['tcid']);
		 $tempdata = del_empty_var(explode('|', $tcid));
		 $tempdata1 = array();
		 $selectstr = '';
		 foreach($tempdata as $key=>$value)
		 {
			$temp = explode(":",$value);
			$tempdata1[$key]['id'] = $temp[0];
			$tempdata1[$key]['name'] = $temp[1];
		 }
		 if(isset($tempdata1[0])&&isset($tempdata1[0]['id']))
		 {
		 $data['table_id'] =  $tempdata1[0]['id'];
		 $data['tablename'] =  $tempdata1[0]['name'];		 
		 }else
		 {
		 $data['table_id'] =  0;
		 $data['tablename'] =  '';		 
		 }		 	

		 if(isset($tempdata1[1])&&isset($tempdata1[1]['id']))
		 {
		    $data['cat_id'] =  $tempdata1[1]['id'];
		    $data['catname'] =  $tempdata1[1]['name'];			 
		 }else
		 {
		    $data['cat_id'] =  0;
		    $data['catname'] =  '';			 
		 }
	 
         $data['add_time'] = time();

 
 		$TB_new = &$this->_model('queuetype');  
				
		$TB_new->editaction($id,$data);
			
		$this->index();  
		 

    }


    function del()
    {

		$id= intval($_GET['id']);
			
		$TB_new = &$this->_model('queuetype');
		$TB_new->del($id);
				      
        $this->index();   

    }
	
    function queuefang()
    {

		$id= intval($_GET['id']);
	    $db = & get_db();
        $order = $db->get_one("SELECT * FROM `".DB_PREFIX."queuemsg` WHERE id = '{$id}' and ispay = 0  LIMIT 1");			
		if($order['uid'])
		{
		 $queuenum = 6 ;
		 $allqueue = $db->get_all("SELECT * FROM `".DB_PREFIX."queuemsg` WHERE queuemoney = '{$order[queuemoney]}' and queueid={$order[queueid]}  ");	
		 $p = $k= 0 ;
		 foreach($allqueue as $key => $value)
		 {
           $p = $p + $queuenum ;
		   if(isset($allqueue[$p]) && $value['id'] == $id )
		   {
		     $k =1 ;break;
		   }			   
		 
		 }			
         if($k)
		 {
		$db->query("UPDATE `".DB_PREFIX."queuemsg` SET `ispay`=1 WHERE id='".$id."'");
		$TB_order = &$this->_model('do'); 
		$TB_order->moneyadd($order['queuemoney'],$order['uid']);
		$ary=array(
			'userid' => $order['uid'],
			'type' => 1,
			'name' => '确认排队返利(后台)',
			'intro' => '您排队返利了'.$order['queuemoney'].'元，由管理员（'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'）确认',
			'money' => $order['queuemoney'],
			'time' => time(),
		);
		$TB_order->moneylog($ary);		 
		 }
		
		}
		
							
        $this->index();   

    }
	
    function queuelist()
    {	
		$id= intval($_GET['id']);
		$td = 0 ;
		$cd = 0 ;
		$pd = 0 ;
		$queuenum = 6 ;		
		$TB_new = &$this->_model('queuetype');
				
		$allqueuetype = $TB_new->allshow();		
        $allqueuetype = $allqueuetype['data'];
		$queuetype_id = array();
        foreach($allqueuetype as $key => $value)
		{
		   if($value['id'] == $id)
		   {
		      $queuetype_id = $value;
		   }
		}
		$td = $queuetype_id['table_id'] ;
		$cd = $queuetype_id['cat_id'] ;
		$pd = $queuetype_id['productid'] ;
		$temp = array();
		$sql = '';
		if($td && $cd && $pd)
		{
		   $sql = " table_id={$td}  and cat_id={$cd} and productid={$pd} ";
		}elseif ($td && $cd && !$pd)
		{
		    $sql = " table_id={$td}  and cat_id={$cd} ";
            foreach($allqueuetype as $key => $value)
		    {
		       if($value['table_id'] == $td && $value['cat_id'] == $cd && $value['productid'])
		       {
		          $sql .= ' and productid !='.$value['productid'].' ';
		       }
		    }		    
		}elseif ($td && !$cd && !$pd)
		{
		    $sql = " table_id={$td}   ";
            foreach($allqueuetype as $key => $value)
		    {
		       if($value['table_id'] == $td && !empty($value['cat_id']) )
		       {
		          $sql .= ' and cat_id !='.$value['cat_id'].' ';
		       }
		    }		
		}elseif (!$td && !$cd && !$pd)
		{
		   $sql = " queuemoney='".$queuetype_id['queuemoney']."' ";		
		}

		$allqueuedata = $TB_new->queueshow('',$sql,$id);

        $this->assign('lw_data', $allqueuedata['data']);		
		$this->assign('lw_page', $allqueuedata['page']);
			
        $this->assign('queuenum', $queuenum);	
		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('queue_list.view.html');   
    }
 	
     function queuekaadd()
    {	
		$id= intval($_GET['id']);
        if(!isset($_GET['do']))
		{
		$_GET['do']='';
		}
		if($_GET['do'] =='do')
		{
		   $num = intval($_POST['num']);
		   $id= intval($_POST['id']);
	       if($num>100 || $num<=0)
	       {
		       $notice = &get_message(array('msg'=>'一次生成最多100张，最少一张','url'=>'?','title'=>'Notice'));
		       $notice ->display();			
	       }		   
		   
		   $TB_new = &$this->_model('queuetype');
		   $data = $TB_new->oneshow($id);			   
		   $queuemoney = $data['queuemoney'];
		   
	       $db = & get_db();
		   for($i=1; $i<=$num ; $i++)
		   {
		       $zhanghao = $this->getkazhanghao();
			   $pwd = date('Ymd',time()).str_pad(rand('1','99999'),5,'0',STR_PAD_LEFT);
			   $time = time();
			   $db->query("INSERT INTO `".DB_PREFIX."queueka` (`queue_id` , `queuemoney` ,`zhanghao` , `pwd` , `add_time`) VALUES 			('$id','$queuemoney','$zhanghao','$pwd','$time')");	
		   }
		       $notice = &get_message(array('msg'=>'生成成功','url'=>'?','title'=>'Notice'));
		       $notice ->display();	
			   exit;		
		}
        $this->assign('id', $id);	
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('queue_kaadd.view.html');   
    }


     function queuekalist()
    {	
		$id= intval($_GET['id']);
        if(!isset($_GET['do']))
		{
		$_GET['do']='';
		}
		if($_GET['do'] =='del')
		{	   

		   $TB_new = &$this->_model('queuetype');
		   $queuedata = $TB_new->oneshowka($id);		   
		   $data = $TB_new->delka($id);
		   $notice = &get_message(array('msg'=>'删除成功','url'=>'?app=queuetype&act=queuekalist&id='.$queuedata['queue_id'],'title'=>'Notice'));
		   $notice ->display();	
		   exit;		
		}
		   $TB_new = &$this->_model('queuetype');
		   $data = $TB_new->queueka($id,'');			
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);		
        $this->assign('id', $id);	
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('queue_kalist.view.html');   
    }

	function getkazhanghao()
	{
		$id=date('Ymd',time()).str_pad(rand('1','99999'),5,'0',STR_PAD_LEFT);
	    $db = & get_db();		
        $order = $db->get_one("SELECT * FROM `".DB_PREFIX."queueka` WHERE zhanghao = '{$id}' LIMIT 1");	
		if(!isset($order['zhanghao']))
		{
			return $id;
		}
		$this->getkazhanghao();
	}		 
}


?>